package com.dpiinc.TechPage.user.logic;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.dpiinc.TechPage.R;
import com.dpiinc.TechPage.UserManager;
import com.dpiinc.TechPage.api.BaseObserver;
import com.dpiinc.TechPage.api.RetrofitFactory;
import com.dpiinc.TechPage.api.RxSchedulers;
import com.dpiinc.TechPage.model.BaseEntity;
import com.dpiinc.TechPage.model.jbean.LabelInfo;
import com.dpiinc.TechPage.model.jbean.NoteLabelEditBean;
import com.dpiinc.TechPage.model.jbean.NoteLabelGetBean;
import com.dpiinc.TechPage.room.RoomAiWriterDatabase;
import com.dpiinc.TechPage.room.bean.LabelEntity;
import com.dpiinc.TechPage.user.utils.ToastUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import io.reactivex.Observable;

/**
 * Created by hu489_000 on 2018-07-29.
 */

public class NoteLabelLogic {

    public interface OnGetSuccess {
        void onHandleSuccess();
    }

    public interface OnEditSuccess {
        void onHandleSuccess(NoteLabelEditBean baseBean);
    }

    public static final String TAG = "NoteLabelLogic";

    /*editType	true	Int	0：新增；1：编辑；2：删除；*/
    public static int EDITTYPE_ADD = 0;
    public static int EDITTYPE_EDIT = 1;
    public static int EDITTYPE_DELETE = 2;


    public static void getNoteLabel(final Context context, final OnGetSuccess onGetSuccess) {
        HashMap<String, String> requestMap = getLabelMap();
        Observable<BaseEntity<NoteLabelGetBean>> observable = RetrofitFactory.getInstance().getLabelList(requestMap);
        BaseObserver mBaseObserver = new BaseObserver<NoteLabelGetBean>(context) {

            @Override
            protected void onHandleError(int code) {
                super.onHandleError(code);
                Log.d(TAG, "getNoteLabel onHandleError code = " + code);
            }

            @Override
            protected void onHandleSuccess(NoteLabelGetBean baseBean) {
                Log.d(TAG, "getNoteLabel onHandleSuccess NoteLabelGetBean = " + baseBean);

                if (baseBean != null && baseBean.getList() != null && baseBean.getList().size() > 0) {
                    List<LabelInfo> list = baseBean.getList();
                    List<LabelEntity> labels = new ArrayList<LabelEntity>();
                    for (LabelInfo li : list) {
                        if (li != null) {
                            LabelEntity le = new LabelEntity(li.getLabelId(), li.getLabelName());
                            labels.add(le);
                        }
                    }
                    if (labels.size() > 0) {

                        RoomAiWriterDatabase.getInstance(context).labelDao().deleteAll();
                        RoomAiWriterDatabase.getInstance(context).labelDao().insert(labels);
                    }

                    if (onGetSuccess != null) {
                        onGetSuccess.onHandleSuccess();
                    }

                }

            }
        };
        observable.compose(RxSchedulers.compose()).subscribe(mBaseObserver);
    }

    private static HashMap<String, String> getLabelMap() {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("uId", UserManager.INSTANCE.getUserId());
        params.put("accessToken", UserManager.INSTANCE.getAccessToken());
        return params;
    }

    public static void noteLabelEdit(final Context context, int editType, String labelId, String labelName, final OnEditSuccess onEditSuccess) {
        HashMap<String, String> requestMap = getLabelEditMap(editType, labelId, labelName);
        Observable<BaseEntity<NoteLabelEditBean>> observable = RetrofitFactory.getInstance().noteLabelEdit(requestMap);
        BaseObserver mBaseObserver = new BaseObserver<NoteLabelEditBean>(context) {

            @Override
            protected void onHandleError(int code) {
                super.onHandleError(code);
                Log.d(TAG, "noteLabelEdit onHandleError code = " + code);
                ToastUtils.showTopTip(context, context.getResources().getString(R.string.handle_error));
            }

            @Override
            protected void onHandleSuccess(NoteLabelEditBean baseBean) {
                Log.d(TAG, "noteLabelEdit onHandleSuccess NoteLabelGetBean = " + baseBean);
                if (onEditSuccess != null) {
                    onEditSuccess.onHandleSuccess(baseBean);
                }

            }
        };
        observable.compose(RxSchedulers.compose()).subscribe(mBaseObserver);
    }

    private static HashMap<String, String> getLabelEditMap(int editType, String labelId, String labelName) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("uId", UserManager.INSTANCE.getUserId());
        params.put("accessToken", UserManager.INSTANCE.getAccessToken());

//        editType	true	Int	0：新增；1：编辑；2：删除；
//        labelId	false	String	标签id。新增时不传，编辑和单个删除时必传
//        labelList	false	String	标签列表[labelId,labelId…]。用于批量删除
//        labelName	false	String	标签名称
        params.put("editType", String.valueOf(editType));
        if(!TextUtils.isEmpty(labelId)){
            params.put("labelId", labelId);
        }
        if(!TextUtils.isEmpty(labelName)) {
            params.put("labelName", labelName);
        }

        return params;
    }
}


